<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>
        8. 设计一个函数DayOfYear(d)，它接收一个日期参数d，返回一个该日期是所在年份的第几天，如DayOfYear(2000,2,8)的返回值是39.
    </h3>
    <h4>（提示：①定义一个数组months=new
        Array（31,28,31,30,31,30,31,31,30,31，30,31）记录每个月少天：②定义一个辅助函数IsLeapYear(y)判定某个年份是否闰年，以确定2月份的天数是28还是29。）</h4>
    <script>
        var day = prompt('请输入日期','2000,2,8');

        function isLeapYear(year) {
            if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
                return true;
            }
            return false
        }
        function DayOfYear(day) {
            var aDay = day.split(',');
            var aMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
            // console.log(aDay);
            // console.log(aDay[0]);
            var days = 0;
            if (isLeapYear(parseInt(aDay[0]))) {
                aMonth[1] = 29;
            }
            for (var i = 0; i < parseInt(aDay[1]) - 1; i++) {
                days += aMonth[i];
            }
            days += parseInt(aDay[2]);
            
            return {
                year: aDay[0],
                days: days
            }
        }

        var result = DayOfYear(day);
        document.write(day + ' 是' + result.year + '年的第' + result.days + '天');
    </script>
</body>

</html>